/**
 * Created by tk6270 on 2016/2/2.
 */
(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        define([], factory);
    } else if (typeof module === 'object' && module.exports) {
        module.exports = factory();
    } else {
        root.TCGNY = factory();
    }
}(this, function () {
    var TCGNY={
        getParamFromUrl:_getParamFromUrl,
        tel400:_tel400,
        resizeGnyImage:_resizeGnyImage
    };

    //获取url参数
    function _getParamFromUrl(name) {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(window.location.href);
        if (results == null) {
            return null;
        }
        else {
            return results[1];
        }
    }

    //#region string prototype / 扩展String方法
    // 验证字符串是否以指定字符串为开头
    String.prototype.startWith = function (s) {
        if (s == null || s == "" || this.length == 0 || s.length > this.length)
            return false;
        if (this.substring(0, s.length) == s)
            return true;
        else
            return false;
        return true;
    };
    // 验证字符串是否以指定字符串为结尾
    String.prototype.endWith = function (s) {
        if (s == null || s == "" || this.length == 0 || s.length > this.length)
            return false;
        if (this.substring(this.length - s.length) == s)
            return true;
        else
            return false;
        return true;
    };
    // 验证字符串是否包含指定字符串
    String.prototype.includeWith = function (s) {
        if (this.length < s.length) {
            return false;
        }
        if (this.indexOf(s) == -1) {
            return false;
        }
        return true;
    };
    // 替换指定字符串
    String.prototype.replaceWith = function (s1, s2) {
        return this.replace(new RegExp(s1, "gm"), s2);
    };
    // 去除首尾空格
    String.prototype.trim = function () {
        return this.replace(/(^\s*)|(\s*$)/g, "");
    };
    /**
     * ie8浏览器 2015-10-16 格式 无法通过 new Date("2015-10-16") 转换为 Date对象
     * 只能支持 new Date("2015/10/16")
     * 故有此方法 将 "2015-10-16" 转换为 "2015/10/16"
     */
    String.prototype.toDateStr = function(){
        return this.replace(/-/g, "/");
    };
    //#endregion

    // refid对应400电话
    function _tel400() {
        // 临时处理，后期更新到gnyCommon --xzj08325 2016/9/19
        var refid = "0";
        var tel = "4007-777-777";
        var refidCookie = /refid=(\d+)(&|$)/gi.exec(gnyCommon.cookie.getItem("CNSEInfo"));
        var refidUrl = /[\?&#]refid=([^\?&#]*)/gi.exec(location.href);

        if (typeof window.getRefid === 'function') {
            refid = window.getRefid();
        }
        if (refidCookie !== null) {
            refid = refidCookie[1];
        }
        if (refidUrl !== null) {
            refid = refidUrl[1];
        }

        if (!refid || refid == "0") return tel;

        var reg = new RegExp("\\|"+refid+"\\|", "gi");
        if (reg.test("|65377704|65377829|65378023|65378188|123153678|123153680|123153681|136980002|126655127|126655131|126655132|126655133|126655125|126655129|136980003|197561617|197561621|197561622|197561625|197561626|")) {
            tel = "4007-970-766";
        }
        else if (reg.test("|65378303|65378402|65378481|65378569|")) {
            tel = "4007-970-788";
        }
        else if (reg.test("|65378675|65378784|65378885|65379005|123152803|123152805|123152806|136979995|136979997|136979998|136979999|136980000|")) {
            tel = "4007-970-799";
        }
        else if (reg.test("|24138644|")) {
            tel = "4007-971-977";
        }
        else if (reg.test("|18210709|")) {
            tel = "4007-998-577";
        }
        else if (reg.test("|12027110|")) {
            tel = "4007-970-977";
        }
        else if (reg.test("|36090387|")) {
            tel = "4001-004-336";
        }
        else if (reg.test("|16777331|")) {
            tel = "4001-003-674";
        }
        else if (reg.test("|17783205|31743810|")) {
            tel = "4001-003-525";
        }
        else if (reg.test("|12034002|52600394|14173680|14748170|14593629|14593679|21538190|49714549|30338112|30338088|22498210|27268941|")) {
            tel = "4007-971-700";
        }
        else if (reg.test("|27284816|27284810|27284827|26571587|30683831|27284800|20077555|24509538|27310819|26571535|26108254|27284852|26571556|30683726|30683796|30683983|30631170|27284865|26571643|")) {
            tel = "4001-004-714";
        }
        else if (reg.test("|47541286|")) {
            tel = "4007-970-055";
        }
        else if (reg.test("|3756892|29562220|44387526|36916893|6983976|16784521|50273806|2818205|26717406|40597456|23767145|27873602|18010403|20026629|52282609|47516825|17858687|21966296|47583458|1085140|1676517|53789862|34847627|4901932|47090876|23627168|20208182|38364663|33432948|33638530|16538761|13989444|1244434|21013674|64767711|35907853|47631516|78760992|14640675|115961602|115961605|")) {
            tel = "4007-840-073";
        }
        else if (reg.test("|33360763|27887570|")) {
            tel = "4006-515-036";
        }
        else if (reg.test("|67793260|67793840|")) {
            tel = "4007-970-900";
        }
        else if (reg.test("|50977156|")) {
            tel = "4007-971-700";
        }
        else if (reg.test("|6928722|14211829|14211860|14211881|14211911|14211945|14211965|14211984|14212122|14212143|14212175|28677675|16818729|29648841|")) {
            tel = "4007-740-075";
        }
        else if (reg.test("|6698041|4140683|")) {
            tel = "4007-972-066";
        }
        else if (reg.test("|12113651|8749630|48255274|")) {
            tel = "4007-998-655";
        }
        else if (reg.test("|17089002|15471599|20077143|44291967|")) {
            tel = "4007-972-088";
        }
        else if (reg.test("|6158806|")) {
            tel = "4007-740-075";
        }
        else if (reg.test("|54559313|")) {
            tel = "4007-995-166";
        }
        else if(reg.test("|136980003|")){
            tel = "4007-970-766"
        }
        else if(reg.test("|6698041|4140683|")){
            tel = "4007-972-066"
        }
        else if(reg.test("|12113651|8749630|48255274|")){
            tel = "4007-998-655"
        }
        else if(reg.test("|17089002|15471599|20077143|44291967|")){
            tel = "4007-972-088"
        }
        else if(reg.test("|181703641|")){
            tel = "4001-001-497"
        }
        else if(reg.test("|77352586|77352587|77352589|77352591|77352592|")){
            tel = "4001-001-943"
        }
        //wap-百度哥伦布样式投放渠道
        else if(reg.test("|239317382|")){
            tel = "4001-004-102"
        }
        // 酷淘流量投放渠道 --xzj08325 2016/9/19 极光2823
        else if(reg.test("|210407349|")){
            tel = "4001-899-449"
        }

        return tel;
    }

    /**
     * 国内游线路图片尺寸重置
     * 可选尺寸如下：
     * 600x400,450x250,254x131,430x270,400x400,600x300,90x90,1080x540,640x320,168x168,640x360,400x300
     * @param  {string} url    图片原始URL
     * @param  {number} width  要优化的宽度
     * @param  {number} height 要优化的高度
     * @return {string}        优化后的图片URL
     */
    function _resizeGnyImage(url, width, height) {
        if (!/gny\/line/gi.test(url) || /_(\d*)x(\d*)_(00|01|02)/gi.test(url)) {
            return url;
        } else {
            return url.replace(/\.(jpg|png|gif)$/i, "_"+width+"x"+height+"_00.$1");
        }
    }

    module.exports = TCGNY ;

    // 暴露公共方法
    return TCGNY;
}));












